
<% include("../common/head.html"){} %>

	<table class="easyui-datagrid" id="roleDataGrid" data-options="url:'/role/query',
		rownumbers:true,pagination:true,pageNumber:1,pageSize:20,pagePosition:'bottom',
		fit:true,toolbar:'#role_search',singleSelect:true,showFooter:true">
		<thead>
			<tr>
				<th data-options="field:'id',width:100">角色编号</th>
				<th data-options="field:'role_name',width:100">角色名称</th>
				<th data-options="field:'description'">角色描述</th>
				
			</tr>
		</thead>
	</table>
	
<div id ="role_search" class="tableSearch">
		<input type="text" id="role_name" size=10  placeholder="角色名称" />
    	<button class="btn btn-default" id="role_submit_search">查 询</button>
    	<button class="btn btn-default" onclick="resetForm();">重 置</button>
    	<br/>
    	<br/>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" onclick="addRole()">新增角色</a> 
		<%}%>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" onclick="editRole()">修改角色</a>
		<%}%>
		<% if(so.hasRole("admin")){%>
			<a href="#" class="easyui-linkbutton" onclick="removeRole()">删除角色</a>
		<%}%> 
		<a href="#" class="easyui-linkbutton" onclick="managePermission()">权限管理</a>
</div>


<div id="dlg" class="easyui-dialog" style="width:400px;height:160px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" novalidate>
            <div class="fitem">
                <label>角色名称:</label>
                <input name="role_name" class="easyui-textbox" required="true" >
            </div>
            <div class="fitem">
                <label>角色描述:</label>
                <input name="description" class="easyui-textbox" required="true" >
            </div>
        </form>
</div>

<div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveRole()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>

<div id="dlgPermission-buttons" class="dialog-button" style="position: relative; top: -1px; width: 377.4px;">
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePermission()" style="width:90px">保存</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgPermission').dialog('close')" style="width:90px">取消</a>
</div> 
    
<div id="dlgPermission" class="easyui-dialog" style="width:400px;height:160px;padding:10px 20px" 
	closed="true" buttons="#dlgPermission-buttons">
        <form id="fmPermission" method="post">
        	<div class="fitem">
                <label>角色名称:</label>
                <input name="role_name" class="easyui-textbox" style="width:200px;" required="true" >
            </div>
            <div class="fitem">
                <label>授权权限:</label>
                <select id="permission_id" class="easyui-combotree" name="permission_id" multiple style="width:200px;"
					data-options="url:'/permission/allPermission'" >
            </div>
        </form>
</div>

    
    <script type="text/javascript">
        var url;
        function addRole(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','新增角色');
            $('#fm').form('clear');
            url = '/role/save';
        }
        function editRole(){
            var row = $('#roleDataGrid').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','修改角色');
                $('#fm').form('load',row);
                url = '/role/edit?id='+row.id;
            }
        }
        function saveRole(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.success){
                        $('#dlg').dialog('close');        // close the dialog
                        $('#roleDataGrid').datagrid('reload');    // reload the  data
                    } else {
                     	$.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }
            });
        }
        function removeRole(){
            var row = $('#roleDataGrid').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','确认删除角色 '+row.role_name,function(r){
                    if (r){
                        $.post('/role/remove',{id:row.id},function(result){
                            if (result.success){
                                $('#roleDataGrid').datagrid('reload');    // reload the  data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
        
        function managePermission(){
        	var row = $('#roleDataGrid').datagrid('getSelected');
            if (row){
            	$('#dlgPermission').dialog('open').dialog('center').dialog('setTitle','权限管理');
            	$('#fmPermission').form('load',row);
            	url = '/role/managePermission?id='+row.id;
            	//获取已经授权的，将其勾选
            	$.post('/role/getPermissions',{id:row.id},function(data){
            		var arrayObj = new Array();
            		$.each(data, function(i, r) {
            			arrayObj.push(r.id);
            		});
            		$('#permission_id').combotree('setValues',arrayObj);
            	});
            }
        }
        function savePermission(){
            $('#fmPermission').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.success){
                        $('#dlgPermission').dialog('close');        // close the dialog
                    } else {
                     	$.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }
            });
        }
    </script>
    
<script>
	$("#role_submit_search").click(function () {
		$('#roleDataGrid').datagrid('load',{
			role_name: $('#role_name').val()
		});
	});
</script>



<% include("../common/footer.html"){} %>